package network;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class EchoServer extends JFrame implements Runnable{

	private JPanel contentPane;
	private JPanel panel;
	private JButton btn2;
	private JButton btn1;
	private JTextField addIn;
	private JScrollPane scrollPane;
	private JTextPane result;

	ServerSocket server = null;
	Socket sock = null;
	boolean threadFlag = true;

	StringBuilder data = new StringBuilder();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					EchoServer frame = new EchoServer();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public EchoServer() {
		setTitle("Echo Server");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 845, 533);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getPanel(), BorderLayout.NORTH);
		contentPane.add(getScrollPane(), BorderLayout.CENTER);
		try{
			InetAddress ia = InetAddress.getLocalHost();
			setTitle(ia.getHostAddress());
		}catch(Exception ei){
			ei.printStackTrace();
		}


	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getBtn2(), BorderLayout.EAST);
			panel.add(getBtn1(), BorderLayout.WEST);
			panel.add(getAddIn(), BorderLayout.CENTER);
		}
		return panel;
	}
	private JButton getBtn2() {
		if (btn2 == null) {
			btn2 = new JButton("Server Stop");
			btn2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					threadFlag = false;
					btn2.setEnabled(false);
					btn1.setEnabled(true);
					data.append("<font size='7' color='#6a5acd'>서버가 중지되었습니다.</font><br/>");
					result.setText(data.toString());

					try{
						if(sock != null ) sock.close();
						if(server != null) server.close();
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			});
		}
		return btn2;
	}
	private JButton getBtn1() {
		if (btn1 == null) {
			btn1 = new JButton("Server Start");
			btn1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try{
						threadFlag = true;
						server = new ServerSocket(4444);
						Thread t = new Thread(EchoServer.this); // 그냥 this 쓰면 actionListener 가 되므로

						t.start();
						btn1.setEnabled(false);
						btn2.setEnabled(true);
						data.append("<font size='7' color='#6a5acd'>서버가 시작됨</font><br/>");
						result.setText(data.toString());

					}catch(Exception ex){

					}
				}
			});
		}
		return btn1;
	}
	private JTextField getAddIn() {
		if (addIn == null) {
			addIn = new JTextField();
			addIn.setColumns(10);
		}
		return addIn;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}
	private JTextPane getResult() {
		if (result == null) {
			result = new JTextPane();
			result.setContentType("text/html");
		}
		return result;
	}

	@Override
	public void run() {
		while(threadFlag){
			try{

				sock = server.accept();
				InetAddress ia = sock.getInetAddress();
				data.append("<b>["+ia.getHostAddress()+"] 클라이언트가 접속되었습니다.</b><br/>");
				result.setText(data.toString());
				
				InputStream is = sock.getInputStream();
				InputStreamReader isr = new InputStreamReader(is);
				BufferedReader br = new BufferedReader(isr);
				
				String str = br.readLine();
				data.append(str);
				
				result.setText(data.toString());
			}catch(Exception ie){
				ie.printStackTrace();
			}
		}

	}
}
